package com.frame.auth.mvc.web;

import com.frame.auth.mvc.dao.mapper.CloudSysUserMapper;
import com.frame.auth.mvc.dao.pojo.CloudSysUser;
import com.frame.auth.mvc.dao.pojo.CloudSysUserExample;
import com.frame.auth.mvc.dao.pojo.vo.UserVO;
import com.frame.common.tools.result.ResultCode;
import com.frame.common.tools.result.ServiceResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther zhangwj
 * @date 2021/3/10 下午7:58
 */
@RestController
@RequestMapping(value = "user")
public class UserController {

    @Resource
    private CloudSysUserMapper cloudSysUserMapper;

    @GetMapping(value = "getUser")
    public ServiceResult<List<UserVO>> getUser(@RequestParam(value = "mobile", required = false) String mobile) {
        CloudSysUserExample cloudSysUserExample = new CloudSysUserExample();
        CloudSysUserExample.Criteria criteria = cloudSysUserExample.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<CloudSysUser> cloudSysUserList = cloudSysUserMapper.selectByExample(cloudSysUserExample);
        if (null != cloudSysUserList && !cloudSysUserList.isEmpty()) {
            List<UserVO> userVOList = new ArrayList<>(cloudSysUserList.size());
            cloudSysUserList.forEach(user -> {
                UserVO userVO = new UserVO();
                userVO.setUserId(user.getUserId());
                userVO.setUserName(user.getUserName());
                userVO.setMobile(user.getMobile());
                userVO.setCreateTime(user.getCreateTime());
                userVOList.add(userVO);
            });
            return ServiceResult.getSuccessResult(userVOList);
        }
        return ServiceResult.getFailureResult(ResultCode.ERROR);
    }
}
